package bat.ke.qq.com.tx;

import bat.ke.qq.com.service.UserSerivce;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * 源码学院-Fox
 * 只为培养BAT程序员而生
 * http://bat.ke.qq.com
 * 往期视频加群:516212256 暗号:6
 */
public class TransactionExample {
    public static void main(String[] args) {
        ClassPathXmlApplicationContext context =
                new ClassPathXmlApplicationContext("spring-tx.xml");
        final UserSerivce userSerivce = context.getBean(UserSerivce.class);


        UserSerivce proxyUserSerivce = (UserSerivce) Proxy.newProxyInstance(
            TransactionExample.class.getClassLoader(),
                new Class[]{UserSerivce.class}, new InvocationHandler() {
                    @Override
                    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                        try {
                            System.out.println("开启事物:"+method.getName());
                            return method.invoke(userSerivce, args);
                        } finally {
                            System.out.println("关闭事物:"+method.getName());
                        }
                    }
                });



    }
}
